#!/usr/bin/env python
#-*- coding:utf-8 -*-

class Byte:
    
    def __init__(self, x = 0):        
        self.bits = [0, 0, 0, 0,  0, 0, 0, 0]
        self.from_decimal(x)
        
    
    def from_decimal(self, x):     
        for i in range(8):
            mask = 1 << i
            bit_ativado = x & mask
            
            if bit_ativado:
                self.set_bit( i+1, 1 )
            else:
                self.set_bit( i+1, 0 )

    def to_decimal(self):
        resp = 0 
        for i in range(8):
            mask = 1 << i
            
            if self.get_bit( i+1 ) != 0:
                resp += mask
        
        return resp

    def get_bit(self, b):
        return self.bits[ 7 - (b-1) ]

    def set_bit(self, b, ativado): 
        self.bits[ 7 - (b-1) ] = ativado

    def to_str(self):
        s = ""
        for b in self.bits:
            s += str( b )
        return s      

